package com.sgb.kjwl.view.ui.adapter.transport.order;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.order.MyOrderEntity;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.widget.self.permission.UserPermissionUtils;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.UserOperating;

import java.util.List;

/**
 * Time:2021/2/5
 * Author:黄淋
 * Description:司机运输订单列表
 */
public class TransportOrderDriverAdapter extends BaseRecycleAdapter<MyOrderEntity> {
    private TransportOrderDriverAdapter.OrderOnClickListener mListener;

    public TransportOrderDriverAdapter(Context context, List<MyOrderEntity> datas) {
        super(context, R.layout.item_transport_order_driver, datas);
    }

    public interface OrderOnClickListener {
        void btnGoClick(String s, MyOrderEntity item);

        void btnOffline(MyOrderEntity entity);
    }

    public void SetOrderListener(TransportOrderDriverAdapter.OrderOnClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void convert(ItemViewHolder holder, MyOrderEntity transportOrderEntity) {
        holder.setText(R.id.order_name, !TextUtils.isEmpty(transportOrderEntity.getCompanyName()) ? transportOrderEntity.getCompanyName() : !TextUtils.isEmpty(transportOrderEntity.getUserName()) ? transportOrderEntity.getUserName() : "");
        holder.setText(R.id.order_driver_address, transportOrderEntity.getCityName() + " " + transportOrderEntity.getDistrictName() + " " + "→" + " " + transportOrderEntity.getDestCityName() + " " + transportOrderEntity.getDestDistrictName());
        holder.setText(R.id.order_driver_operation, transportOrderEntity.getTotalDistance());
        holder.setText(R.id.order_driver_goods_detail, (TextUtils.isEmpty(CargoConstant.getLastGoodsTypeName(transportOrderEntity.getGoodsModeName(), transportOrderEntity.getSecondGoodsTypeName())) ? "" : CargoConstant.getLastGoodsTypeName(transportOrderEntity.getGoodsModeName(), transportOrderEntity.getSecondGoodsTypeName()) + "  ") + (TextUtils.isEmpty(transportOrderEntity.getWeight()) || "0".equals(NumberUtil.trim0(transportOrderEntity.getWeight())) ? "" : NumberUtil.trim0(transportOrderEntity.getWeight()) + "吨  ") + ("0".equals(NumberUtil.trim0(transportOrderEntity.getVolume())) || TextUtils.isEmpty(transportOrderEntity.getVolume()) ? "" : NumberUtil.trim0(transportOrderEntity.getVolume()) + "方"));
        holder.setText(R.id.order_driver_time, CargoConstant.stampFormatListLoadTime(true, transportOrderEntity.getShipmentTime()
                , transportOrderEntity.getStartPointTime(), transportOrderEntity.getShipmentEndTime(), transportOrderEntity.getEndPointTime()));

        //信息费为0，不显示
        if (TextUtils.isEmpty(transportOrderEntity.getMessagePrice()) || NumberUtil.trim0(transportOrderEntity.getMessagePrice()).equals("0")) {
            holder.setVisible(R.id.message_price_layout, false);
        } else {
            holder.setVisible(R.id.message_price_layout, true);
            holder.setText(R.id.order_driver_messagePrice, NumberUtil.trim0(transportOrderEntity.getMessagePrice()) + "元");
        }
        holder.setText(R.id.order_driver_allFreight, NumberUtil.trim0(transportOrderEntity.getAllFreight()) + "元");
        String carInfo = "";
        if (!TextUtils.isEmpty(transportOrderEntity.getCarNumber())) {
            carInfo += transportOrderEntity.getCarNumber() + "/";
        }
        if (!TextUtils.isEmpty(transportOrderEntity.getCarTypeName())) {
            carInfo += transportOrderEntity.getCarTypeName() + "/";
        }
        if (!TextUtils.isEmpty(transportOrderEntity.getCarLenName())) {
            carInfo += transportOrderEntity.getCarLenName() + "/";
        }
        holder.setText(R.id.order_driver_carinfo, !TextUtils.isEmpty(carInfo) ? carInfo.substring(0, carInfo.length() - 1) : "/");
        if (transportOrderEntity.getQuotationOfferType()!=null&&transportOrderEntity.getQuotationOfferType()==3){//个人报价才展示车辆信息
            holder.setVisible(R.id.car_info_layout,true);
        }else {
            holder.setVisible(R.id.car_info_layout,false);
        }
        setState(holder, transportOrderEntity.getStatus(), transportOrderEntity);
        SetOnclick(holder, transportOrderEntity);
    }

    /**
     * 设置状态
     *
     * @param state 6待货主付款 1待确认  2待发车 3运输中 4已完成 5已取消
     */
    @SuppressLint("ResourceAsColor")
    private void setState(ItemViewHolder holder, Integer state, MyOrderEntity transportOrderEntity) {
        holder.setVisible(R.id.btn_go, true);
        holder.getView(R.id.message_pay_state).setVisibility(View.GONE);
        switch (state) {
            case 0:
                if (UserOperating.getInstance().isCompany()){
                    holder.getView(R.id.rl_btn_next).setVisibility(View.GONE);
                    holder.getView(R.id.line_view).setVisibility(View.GONE);
                }else {
                    holder.getView(R.id.rl_btn_next).setVisibility(View.VISIBLE);
                    holder.getView(R.id.line_view).setVisibility(View.VISIBLE);
                }
                holder.getView(R.id.btn_offline).setVisibility(View.GONE);
                holder.getView(R.id.message_pay_state).setVisibility(View.VISIBLE);
                holder.setText(R.id.btn_go, "支付信息费");//企业不提供支付信息费功能
                holder.setText(R.id.order_state_tv, "待货主确认");
                ((TextView) (holder.getView(R.id.order_state_tv))).setTextColor(Color.parseColor("#5792FD"));
                break;

            case 1:
                holder.getView(R.id.rl_btn_next).setVisibility(View.GONE);
                holder.getView(R.id.line_view).setVisibility(View.GONE);
                holder.getView(R.id.btn_offline).setVisibility(View.GONE);
                holder.setText(R.id.order_state_tv, "待货主确认");
                ((TextView) (holder.getView(R.id.order_state_tv))).setTextColor(Color.parseColor("#5792FD"));
                break;
            case 2:
                holder.getView(R.id.rl_btn_next).setVisibility(View.VISIBLE);
                holder.getView(R.id.line_view).setVisibility(View.VISIBLE);
                holder.getView(R.id.btn_offline).setVisibility(View.GONE);
                holder.getView(R.id.pay_meassage).setVisibility(View.INVISIBLE);
                holder.getView(R.id.pay_meassage_freight).setVisibility(View.INVISIBLE);
                holder.setText(R.id.btn_go, "确认发车");
                holder.setText(R.id.order_state_tv, "待司机发车");
                ((TextView) (holder.getView(R.id.order_state_tv))).setTextColor(Color.parseColor("#5792FD"));
                break;
            case 3:
                holder.getView(R.id.rl_btn_next).setVisibility(View.VISIBLE);
                holder.getView(R.id.line_view).setVisibility(View.VISIBLE);
                holder.getView(R.id.btn_offline).setVisibility(View.GONE);
                holder.getView(R.id.pay_meassage).setVisibility(View.INVISIBLE);
                holder.getView(R.id.pay_meassage_freight).setVisibility(View.INVISIBLE);
                holder.setText(R.id.btn_go, "确认送达");
                holder.setText(R.id.order_state_tv, "运输中");
                ((TextView) (holder.getView(R.id.order_state_tv))).setTextColor(Color.parseColor("#5792FD"));
                break;
            case 6:
                holder.getView(R.id.rl_btn_next).setVisibility(View.VISIBLE);
                holder.getView(R.id.line_view).setVisibility(View.VISIBLE);
                holder.setText(R.id.btn_go, "收款二维码");
                holder.setVisible(R.id.btn_go, false);
                holder.getView(R.id.btn_offline).setVisibility(View.VISIBLE);
                holder.getView(R.id.pay_meassage).setVisibility(View.VISIBLE);
                holder.getView(R.id.pay_meassage_freight).setVisibility(View.VISIBLE);
                holder.setText(R.id.pay_meassage, "待收运费： ");
                holder.setText(R.id.pay_meassage_freight, NumberUtil.trim0(transportOrderEntity.getAllFreight()) + "元");
                holder.setText(R.id.order_state_tv, "待货主支付运费");
                ((TextView) (holder.getView(R.id.order_state_tv))).setTextColor(R.color.color_FFC000);

                break;
            case 4:
                holder.getView(R.id.rl_btn_next).setVisibility(View.GONE);
                holder.getView(R.id.line_view).setVisibility(View.GONE);
                holder.getView(R.id.btn_offline).setVisibility(View.GONE);
                holder.setText(R.id.order_state_tv, "已完成");
                ((TextView) (holder.getView(R.id.order_state_tv))).setTextColor(R.color.color_06C764);
                break;
        }
    }

    /**
     * 点击事件
     */
    private void SetOnclick(ItemViewHolder helper, MyOrderEntity item) {
        TextView btn_go = helper.getView(R.id.btn_go);
        helper.getView(R.id.btn_go).setOnClickListener(v -> {
            if (mListener != null)
                mListener.btnGoClick(btn_go.getText().toString(), item);
        });
        helper.getView(R.id.btn_offline).setOnClickListener(v -> {
            if (mListener != null) {
                mListener.btnOffline(item);
            }
        });
    }
}
